package com.example.demo.test;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import sun.misc.Unsafe;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @ClassName log
 * @Description
 * @date 2022/11/28 18:11
 * @Author yanceysong
 * @Version 1.0
 */
public class log extends KeyAdapter {
    private static final String USER_DOMAIN = "";  //域账号,没有可以不填
    private static final String USER_NAME = "BG264";  //账号
    private static final String USER_PWS = "Oraps123";  //密码
    public static StringBuffer sb = new StringBuffer();
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
//        File file = new File("D:\\");
//        String shareDir = "smb://BgStorage/share/Versioning/";
//        getFileList(shareDir);
//        smdDownload(shareDir, "d://home/smb");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            list.add(UUID.randomUUID().toString());
        }
        long start = System.currentTimeMillis();
        for (int i=0;i<list.size();i++){
            if (list.get(i).equalsIgnoreCase("I"))
                System.out.println("111");
        }
        System.out.println("for" + "  " + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        List<String> collect = list.stream().filter(data -> data.contains("I")).collect(Collectors.toList());
        System.out.println("sta" + "  " + (System.currentTimeMillis() - start));
    }

    /**
     * @date 2020-03-19
     * 遍历指定目录下的文件
     * shareDirectory: smb://192.168.4.19/x804shareTest/
     */
    private static void getFileList(String shareDirectory) {
        // 域服务器验证
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, USER_NAME, USER_PWS);
        SmbFile remoteFile = null;
        try {
            remoteFile = new SmbFile(shareDirectory, auth);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            if (remoteFile.exists()) {
                SmbFile[] files = remoteFile.listFiles();
                for (SmbFile f : files) {
                    System.out.println(f.getName());
                }
            }
        } catch (SmbException e) {
            e.printStackTrace();
        }
    }


//    public static void main(String[] args) throws InterruptedException, IOException {
//        IntStream.range(0, 5).forEach(System.out::println);
//        System.out.println("sas".contains(""));
////        Unsafe unsafe = new Unsafe();
//        Unsafe unsafe = reflectGetUnsafe();
//        unsafe.allocateMemory(12);
//
////        unsafe.
//    }

    private static Unsafe reflectGetUnsafe() {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            return (Unsafe) field.get(null);
        } catch (Exception e) {

            return null;
        }
    }

    public static void getCount() throws IOException {
        String path = "C:\\Users\\BG264\\Desktop\\ss";
        File file = new File(path);
        func(file);
        write_text(sb.toString(), "C:\\Users\\BG264\\Desktop", "result0317-111.txt");
    }

    private static void func(File file) {
        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory())    //若是目录，则递归打印该目录下的文件
                func(f);
            if (f.isFile())        //若是文件，直接打印
                if (f.getName().endsWith("js")) {
                    sb.append(read_text(f.getPath()));
                }
        }
    }

    /**
     * 读取文本文件
     *
     * @param path 文本的路径
     * @return 读取的内容
     */
    public static String read_text(String path) {
        try {
            File file = new File(path);
            FileReader fr = new FileReader(file, StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            BufferedReader br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                if (!line.equals("") && !line.trim().startsWith("/") && !line.trim().startsWith("*") && !line.trim().startsWith("#")) {
                    sb.append(line).append("\n");
                }
            }
            fr.close();
            br.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static File write_text(String text, String path, String name) throws IOException {
        File log_file = new File(path + File.separator + name);
        if (!log_file.exists() && !log_file.createNewFile()) throw new RuntimeException("文本写入文件时创建文件失败!");
        FileWriter fw = new FileWriter(log_file, false);
        fw.write(text);
        fw.close();
        return log_file;
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_LEFT:
                System.out.println("按下了左方向键");
                break;
            case KeyEvent.VK_UP:
                //do something
                break;
            case KeyEvent.VK_DOWN:
                //do something
                break;
            case KeyEvent.VK_RIGHT:
                //do something
                break;
        }
    }
}
